using System;
using System.Collections.Generic;
using System.Windows.Forms;
using System.IO;
using System.Text;
using System.Diagnostics;

namespace BatLauncher
{
    static class Program
    {
        /// <summary>
        /// The main entry point for the application.
        /// </summary>
        [STAThread]
        static void Main(string[] args)
        {
            Application.EnableVisualStyles();
            Application.SetCompatibleTextRenderingDefault(false);
            Application.Run(new Form1(args));
        }
    }

    class Form1:Form
    {
        #region Windows Form Designer generated code
        /// <summary>
        /// Required designer variable.
        /// </summary>
        private System.ComponentModel.IContainer components = null;

        /// <summary>
        /// Clean up any resources being used.
        /// </summary>
        /// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param>
        protected override void Dispose(bool disposing)
        {
            if (disposing && (components != null))
            {
                components.Dispose();
            }
            base.Dispose(disposing);
        }


        /// <summary>
        /// Required method for Designer support - do not modify
        /// the contents of this method with the code editor.
        /// </summary>
        private void InitializeComponent()
        {
            this.SuspendLayout();
            // 
            // Form1
            // 
            this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
            this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
            this.ClientSize = new System.Drawing.Size(1, 1);
            this.FormBorderStyle = FormBorderStyle.None;
            this.Name = "Form1";
            this.Text = "Form1";
            this.Load += new System.EventHandler(this.Form1_Load);
            this.ResumeLayout(false);

        }

        #endregion

        bool hide = false;

        public Form1(string[] args)
        {
            InitializeComponent();
            this.Visible = false;
            extract();
            start(args);
        }

        private string buildargument(string[] args)
        {
            StringBuilder arg = new StringBuilder();
            for (int i = 0; i < args.Length; i++)
            {
                arg.Append(args[i] + " ");
            }

            return arg.ToString();
        }

        private void extract()
        {
            string name = System.Reflection.Assembly.GetExecutingAssembly().GetManifestResourceNames()[0];
            hide = name.EndsWith("hideit.bat");

            Stream theResource = System.Reflection.Assembly.GetExecutingAssembly().GetManifestResourceStream(name);

            BinaryReader br = new BinaryReader(theResource);
            FileStream fs = new FileStream(Environment.GetEnvironmentVariable("TEMP") + "\\it.bat", FileMode.Create);

            byte[] bt = new byte[theResource.Length];
            theResource.Read(bt, 0, bt.Length);
            fs.Write(bt, 0, bt.Length);

            br.Close();
            fs.Close();
        }

        private void start(string[] args)
        {
            ProcessStartInfo info = new ProcessStartInfo();
            info.FileName = Environment.GetEnvironmentVariable("TEMP") + "\\it.bat";
            info.Arguments = buildargument(args);
            info.CreateNoWindow = hide;
            if (hide)
            {
                info.WindowStyle = ProcessWindowStyle.Hidden;
            }
            info.WorkingDirectory = Application.StartupPath;

            Process proc = new Process();
            proc.StartInfo = info;
            proc.Start();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            Application.Exit();
        }
    }
}